package com.vxb.testmvp.test_zonghe.test_3;

import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.holenzhou.pullrecyclerview.PullRecyclerView;
import com.holenzhou.pullrecyclerview.layoutmanager.XLinearLayoutManager;
import com.vxb.testmvp.R;
import com.vxb.testmvp.test_zonghe.network.NetworkManager;
import com.vxb.testmvp.util.SPUtils;
import com.vxb.testmvp.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class Test3Activity extends AppCompatActivity {

    @Bind(R.id.pullrecycler_view)
    PullRecyclerView mPullRecyclerView;
    private CompositeSubscription mCompositeSubscription;
    private List<ScoreBase.ScoreData.ScoreContent.Detail> mDataList = new ArrayList<>();
    private Test3Adapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test3);

        ButterKnife.bind(this);
        mCompositeSubscription = new CompositeSubscription();
        initView();
    }

    int mPageNum = 1;
    int zongYeShu = 5;

    private void initData() {
        String token = (String) SPUtils.get("token", "", this);

        mCompositeSubscription.add(NetworkManager.V2Service()
                .getScoreDetail(token, String.valueOf(mPageNum))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Action1<ScoreBase>() {
                            @Override
                            public void call(ScoreBase scoreBase) {

                                zongYeShu = 5;

                                mDataList.addAll(scoreBase.getScoreData().getScoreContent().get(0).detailList);
                                mAdapter.notifyDataSetChanged();
                                mPullRecyclerView.stopRefresh();
                                mPullRecyclerView.enableLoadMore(mPageNum < zongYeShu ? true : false);

                                mPullRecyclerView.stopLoadMore();
                            }
                        },
                        new Action1<Throwable>() {

                            @Override
                            public void call(Throwable throwable) {
                                throwable.printStackTrace();

                                mAdapter.notifyDataSetChanged();

                                mPullRecyclerView.stopRefresh();
                                mPullRecyclerView.enablePullRefresh(false);

                                mPullRecyclerView.stopLoadMore();

                                Util.showToast(getApplication(), "加载错误,网络出问题了.");
                                //  Util.makeSnck(mPullRecyclerView,"网络出问题了");
                                Snackbar.make(mPullRecyclerView, "网络出问题了", Snackbar.LENGTH_INDEFINITE)
                                        .setAction("重试", new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                mPullRecyclerView.postRefreshing();
                                                mPullRecyclerView.enablePullRefresh(true);
                                            }
                                        })
                                        .show();
                            }
                        }));


    }

    private void initView() {

        // 初始化PullRecyclerView
        mPullRecyclerView.setLayoutManager(new XLinearLayoutManager(Test3Activity.this));

        mAdapter = new Test3Adapter(this, R.layout.list_item_my_score, mDataList);
        mPullRecyclerView.setAdapter(mAdapter);


        mPullRecyclerView.setColorSchemeResources(R.color.colorAccent); // 设置下拉刷新的旋转圆圈的颜色

        mPullRecyclerView.enablePullRefresh(true); // 开启下拉刷新，默认即为true，可不用设置
        mPullRecyclerView.enableLoadDoneTip(true, R.string.load_done_tip); // 开启数据全部加载完成时的底部提示，默认为false
        mPullRecyclerView.setOnRecyclerRefreshListener(new PullRecyclerView.OnRecyclerRefreshListener() {
            @Override
            public void onPullRefresh() {
                mPageNum = 1;
                mDataList.clear();

                initData();
                //             mPullRecyclerView.enableLoadMore(true); // 当剩余还有大于0页的数据时，开启上拉加载更多
                //           pageSize = 2;
            }

            @Override
            public void onLoadMore() {

                mPageNum++;
                initData();

            }
        });
        mPullRecyclerView.postRefreshing();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }


}
